﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Collections;

namespace QLBN
{
    class Users
    {

        DBConnector dbc = new DBConnector();
        String _username;

        public String Username
        {
            get { return _username; }
            set { _username = value; }
        }
        String _pass;

        public String Pass
        {
            get { return _pass; }
            set { _pass = value; }
        }
        String _hoten;

        public String Hoten
        {
            get { return _hoten; }
            set { _hoten = value; }
        }
        String _diachi;

        public String Diachi
        {
            get { return _diachi; }
            set { _diachi = value; }
        }
        String _role;

        public String Role
        {
            get { return _role; }
            set { _role = value; }
        }

        public DataSet Lay_Thong_Tin()
        {
            String sql = "Select * from NguoiDung";
            DataSet ds = dbc.executeQuery(sql);
            return ds;
        }

        public void Lay_Thong_Tin(String username)
        {
            String sql = "Select * from NguoiDung where UserName = '" + username + "'";
            DataSet ds = dbc.executeQuery(sql);
            ds = dbc.executeQuery(sql);

            if (ds != null)
            {
                _username = ds.Tables[0].Rows[0]["UserName"].ToString();
                _pass = ds.Tables[0].Rows[0]["Pass"].ToString();
                _diachi = ds.Tables[0].Rows[0]["DiaChi"].ToString();
                _hoten = ds.Tables[0].Rows[0]["HoTen"].ToString();
                _role = ds.Tables[0].Rows[0]["Role"].ToString();
            }
        }

        public Boolean Them_User()
        {
            Boolean ck = false;
            String sql = "Select UserName from NguoiDung";
            OleDbDataReader reader = dbc.executeReader(sql);
            while (reader.Read())
            {
                if (Comparer.Equals(reader["UserName"].ToString(), Username))
                {

                    MessageBox.Show("Không thể sử dụng tên đăng nhập này !");
                    ck = false;
                    break;
                }
                ck = true;
            }
            if (ck == true)
            {
                sql = "Insert into NguoiDung ";
                sql += "values ('" + Username + "','" + Pass + "','" + Role + "','" + Hoten + "','" + Diachi + "')";
                dbc.executeNonQuery(sql);
                return true;
            }
            return false;
        }

        public Boolean Cap_Nhat_User(String username)
        {
            Boolean ck = false;
            String sql = "Select UserName from NguoiDung";
            OleDbDataReader reader = dbc.executeReader(sql);
            while (reader.Read())
            {
                if (Comparer.Equals(reader["UserName"].ToString(), Username))
                {

                    if (username != Username)
                    {
                        MessageBox.Show("Không thể sử dụng tên đăng nhập này !");
                        ck = false;
                        return false;

                    }
                }
                ck = true;
            }
            if (ck == true)
            {
                sql = "Update NguoiDung set UserName='" + Username + "', Pass='" + Pass + "',Role = '" + Role + "',HoTen = '" + Hoten + "',DiaChi = '" + Diachi + "' ";
                sql += "Where UserName = '" + username + "'";
                //MessageBox.Show(sql);
                dbc.executeNonQuery(sql);
                return true;
            }
            return false;
        }

        public Boolean Xoa_User(String username)
        {
            try
            {
                Boolean ck;
                if (Comparer.Equals(_username, username))
                {
                    MessageBox.Show("Không thể xóa user này !");
                    ck = false;
                    return false;
                }
                else
                {
                    ck = true;
                }
                if (ck == true)
                {
                    MessageBox.Show("AAA");
                    String sql = "Delete From NguoiDung Where UserName = '" + username + "'";
                    dbc.executeNonQuery(sql);
                    MessageBox.Show("hhh");
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            } 
            return false;
        }
    }
}
